//
//  MianViewController.swift
//  ZSYWeiBo
//
//  Created by 周饰宇 on 2017/4/12.
//  Copyright © 2017年 zhouShiYu. All rights reserved.
//

import UIKit

class MianViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        print(tabBar) // tabBar有
        print(tabBar.items ?? "items为空") // tabBar.items没有
        
        // 替换系统tabBar
        // 替换系统tabBar可以在viewDidLoad中来动手脚
        // 系统tabBar是只读的，所以我们不能直接替换，可以利用KVC来替换
        // 获取tabBar
        let tb = MianTabBar()
        setValue(tb, forKey: "tabBar")
        
        tb.composeButton.addTarget(self, action:#selector(MianViewController.composeBtnClick) , for: UIControlEvents.touchUpInside)
        
        
        addChildViewControllers()
        
    }
    
    func composeBtnClick() {
        
        print(#function)
    }
    
    override func viewWillAppear(_ animated: Bool) {
        
        super.viewWillAppear(animated)
        
        print(tabBar.items ?? "items为空")
    }
    
    private func addChildViewControllers(){

        // 1.加载json
        let path = Bundle.main.path(forResource: "MainVCSettings.json", ofType: nil)
        guard let jsonData = try? Data(contentsOf: URL(fileURLWithPath: path!)) else {
            print("没有获取到json文件中数据")
            return
        }
        
        do{
            
            let array = try JSONSerialization.jsonObject(with: jsonData, options: JSONSerialization.ReadingOptions.allowFragments)
            
            for dict in array as! [[String : String]]{
                
                addChildViewController(vcName: dict["vcName"]!, title: dict["title"]!, imageName: dict["imageName"]!)
            }
        }catch {
            
            print(error)
            // 如果json出差，直接字符串加载
    
            addChildViewController(vcName: "HomeTableViewController", title:"首页", imageName:"tabbar_home")
    
            addChildViewController(vcName: "MessageTableViewController", title: "消息", imageName: "tabbar_message_center")
    
            addChildViewController(vcName: "DiscoverTableViewController", title: "发现", imageName: "tabbar_discover")
    
            addChildViewController(vcName: "ProfileTableViewController", title: "我", imageName: "tabbar_profile")
        }
        
//        var str :String? = nil
//            
//        do{
//            
//            str = try NSString(contentsOfFile: path!,encoding: String.Encoding.ascii.rawValue) as String
//            
//        }catch{
//            
//            
//            
//        }
        

        

        
    }
    
    
    // 添加控制器
    private func addChildViewController(vcName: String, title:String, imageName:String) {
        
        tabBar.tintColor = UIColor.orange
        
        // 在swift 中，类名是包含命名空间的
        // 命名空间默认是项目名称，同一个命名空间全局共享
        
        // info.list信息
        // 在swift中，通常设定对象的类型，写在‘as! String’ 'as? String'
        // 都是自动推导，右侧制定好，左侧变量类型自然OK
            let ns = Bundle.main.infoDictionary!["CFBundleExecutable"] as! String
        
        let cls : AnyClass = NSClassFromString(ns + "." + vcName)!
        print(cls)
        
        
        let vc = cls.alloc() as! UIViewController
        
        // 从内向外设置 nav & tabbar
        vc.title = title
        vc.tabBarItem.image = UIImage(named : imageName)
        vc.tabBarItem.selectedImage = UIImage(named: imageName + "_highlighted")
        let nav = UINavigationController(rootViewController : vc)
        addChildViewController(nav)
    }

    
    /*
    // 添加控制器
    private func addChildViewController(vc: UIViewController, title:String, imageName:String) {
        
        tabBar.tintColor = UIColor.orange
        
        // 从内向外设置 nav & tabbar
        vc.title = title
        vc.tabBarItem.image = UIImage(named : imageName)
        vc.tabBarItem.selectedImage = UIImage(named: imageName + "_highlighted")
        let nav = UINavigationController(rootViewController : vc)
        addChildViewController(nav)
    }
    
    */
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
